<template>
  <div style="background-color: #FFFFFF; padding: 20px;">
    <div>
      <div style="margin: 0 0 10px;overflow: hidden">
        <div style="float: left;">
          <router-link :to="{name: EditName}">
            <el-button type="primary" plain>添加</el-button>
          </router-link>
          <el-button type="danger" plain @click="delObjs()">删除</el-button>
          <el-button type="primary" :disabled="!is_show" plain @click="inputFunc()">查询</el-button>
          <el-switch style="padding-left: 15px" v-model="is_show"></el-switch>
        </div>
      </div>

      <div v-if="is_show">
        <!--项目-->
        <div class="input-data">
          <el-select
            v-model="optionsProjectId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入项目名称"
            :remote-method="getProjectInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in project_info"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--模块-->
        <div class="input-data">
          <el-select
            v-model="optionsModuleId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入模块"
            :remote-method="getModuleInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in module_info"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--页面-->
        <div class="input-data">
          <el-select
            v-model="optionsModulePageId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入页面"
            :remote-method="getModulePageInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in module_page_info"
              :key="index"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--步骤-->
        <div class="input-data">
          <el-select
            v-model="optionsStepsId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入步骤"
            :remote-method="getStepsInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in steps_info"
              :key="index"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <!--函数-->
        <div class="input-data">
          <el-select
            v-model="optionsSeleniumFuncId"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入函数"
            :remote-method="getSeleniumFuncInfo"
            :loading="loading">
            <el-option
              v-for="(item,index) in selenium_func_info"
              :key="index"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <div class="input-data">
          <el-select v-model="results" placeholder="执行状态">
            <el-option
              v-for="(item,index) in optionsResults"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <div class="input-data">
          <el-select v-model="status" placeholder="请选择类型">
            <el-option
              v-for="(item,index) in optionsCommon"
              :key="index"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </div>

        <div class="input-data">
          <el-input placeholder="请输入obj_key" v-model="obj_key"></el-input>
        </div>

        <div class="input-data">
          <el-input placeholder="请输入步骤标题" v-model="inputObjName"></el-input>
        </div>
      </div>

      <div>
        <!--内容组件-->
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange">

          <el-table-column
            type="selection"
            width="55">
          </el-table-column>

          <el-table-column
            prop="id"
            label="ID"
            width="100">
          </el-table-column>

          <el-table-column
            prop="title"
            label="标题"
            width="200">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.title"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="obj_key"
            label="KEY"
            width="200">
          </el-table-column>

          <el-table-column
            label="所属项目"
            width="180">
            <template slot-scope="scope">
              <el-popover v-if="scope.row.project_info" trigger="hover" placement="top">
                <p>项目ID: {{ scope.row.project_info.id }}</p>
                <p>项目名称: {{ scope.row.project_info.name }}</p>
                <p>项目描述: {{ scope.row.project_info.status }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.project_info.value }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="所属模块"
            width="180">
            <template slot-scope="scope">
              <el-popover v-if="scope.row.module_info" trigger="hover" placement="top">
                <p>模块ID: {{ scope.row.module_info.id }}</p>
                <p>模块名称: {{ scope.row.module_info.name }}</p>
                <p>模块状态: {{ scope.row.module_info.status }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.module_info.value }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="所属页面"
            width="180">
            <template slot-scope="scope">
              <el-popover v-if="scope.row.module_page_info" trigger="hover" placement="top">
                <p>页面ID: {{ scope.row.module_page_info.id }}</p>
                <p>页面名称: {{ scope.row.module_page_info.title }}</p>
                <p>页面状态: {{ scope.row.module_page_info.status }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.module_page_info.value }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="所属步骤"
            width="180">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>ID: {{ scope.row.steps_info.id }}</p>
                <p>标题: {{ scope.row.steps_info.title }}</p>
                <p>状态: {{ scope.row.steps_info.status }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.steps_info.value }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            label="使用函数"
            width="180">
            <template slot-scope="scope">
              <el-popover trigger="hover" placement="top">
                <p>ID: {{ scope.row.selenium_func_info.id }}</p>
                <p>标题: {{ scope.row.selenium_func_info.title }}</p>
                <p>函数名: {{ scope.row.selenium_func_info.func_name }}</p>
                <p>状态: {{ scope.row.selenium_func_info.status }}</p>
                <div slot="reference" class="name-wrapper">
                  <el-tag size="medium">{{ scope.row.selenium_func_info.value }}</el-tag>
                </div>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column
            prop="into_sql_push"
            label="查询sql入参"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.into_sql_push"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_sql_push"
            label="断言sql入参"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.assert_sql_push"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="into_template"
            label="函数入参模板"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.into_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="out_template"
            label="校验出参模板"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.out_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_template"
            label="断言模板"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.assert_template"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="into_sql_pull"
            label="查询sql出参"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.into_sql_pull"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_sql_pull"
            label="断言sql出参"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.assert_sql_pull"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="into_data"
            label="函数入参数据"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.into_data"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="page_out_data"
            label="页面数据"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.page_out_data"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="out_data"
            label="被校验数据"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.out_data"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="assert_data"
            label="断言数据"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.assert_data"></div>
            </template>
          </el-table-column>


          <el-table-column
            prop="result_data"
            label="对比结果"
            width="250">
            <template slot-scope="scope">
              <div class="mytable" v-text="scope.row.result_data"></div>
            </template>
          </el-table-column>

          <el-table-column
            prop="failure_img"
            label="失败截图"
            width="100">
            <template slot-scope="scope">
              <div v-show="scope.row.failure_img">
                <el-link type="success" :href="getUrl(scope.row.failure_img)" target="_blank">查看</el-link>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            prop="create_time"
            label="创建日期"
            width="200">
          </el-table-column>

          <el-table-column
            prop="update_time"
            label="更新日期"
            width="200">
          </el-table-column>

          <el-table-column prop="show_status" label="显示全部结果" width="120">
            <template slot-scope="scope">
              <el-switch
                disabled
                v-model="scope.row.show_status">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="status" label="步骤顺序" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.sort" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsSort"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column fixed="right" prop="status" label="执行状态" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.results" disabled placeholder="请选择">
                <el-option
                  v-for="(item,index) in optionsResults"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column prop="status" label="类型" width="120">
            <template slot-scope="scope">
              <el-select size="mini" v-model="scope.row.status" disabled placeholder="请选择类型">
                <el-option
                  v-for="(item,index) in optionsCommon"
                  :key="index"
                  :label="item.value"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>


          <el-table-column
            fixed="right"
            label="操作"
            width="200">
            <template slot-scope="scope">
              <router-link :to="{name: 'SeleniumFunc', query: { selenium_func_id: scope.row.selenium_func_info.id }}">
                <el-button type="text" size="small">函数</el-button>
              </router-link>
              <el-button :disabled="scope.row.status===1" type="text" size="small"
                         @click="stepsRealizeAscension(scope.row.id)">步骤提升
              </el-button>

              <router-link :to="{name: EditName, query: { steps_realize_id: scope.row.id }}">
                <el-button type="text" size="small">编辑</el-button>
              </router-link>
              <el-button :disabled="scope.row.status===1" type="text" size="small" @click="delObjs(scope.row.id)">删除
              </el-button>

            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <div class="block paging">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'StepsRealize',
    data() {
      return {

        // 搜索功能实现
        is_show: false,
        status: '',  // 状态
        results: '',  // 执行状态
        obj_key: '',  // objkey


        // 需要搜索的项目名称
        inputObjName: '',  // 搜索功能实现的字符串
        // choices设置
        optionsCommon: [],  // 存储CommonChoices实际数据的列表
        optionsResults: [],  // 执行状态
        optionsSort: [],  // 步骤顺序

        loading: false,
        optionsProjectId: [],
        optionsModuleId: [],
        optionsModulePageId: [],
        optionsStepsId: [],
        optionsSeleniumFuncId: [],

        project_info: [], // 项目列表，获取1000条项目数据
        module_info: [], // 模块列表，获取1000条项目数据
        module_page_info: [], // 页面列表
        steps_info: [], // 模块列表，获取1000条项目数据
        selenium_func_info: [], // 页面列表


        // 分页设置
        currentPage: 1, //当前页码
        pageSize: 10,    //    每页的条数
        pageSizes: [10, 20, 30, 40],  // 可选每页显示条数
        dataCount: 0,  // 数据总条数

        // 数据设置
        ListDataURL: this.$http.StepsRealizeURL,  // 表格列表数据获取
        EditName: 'EditStepsRealize',
        objID: 'steps_realize_id',
        tableData: [],  // 阿贾克斯获取的初始数据
        multipleSelection: [],  // 不能删，我也不知道是干嘛用的，删了会报错
      }
    },
    created() {
      // 发送啊假克丝请求
      this.objList();// 阿贾克斯获取的初始数据
      this.optionsCommon = this.getChoices.common;  // 获取标准CHOICES
      this.optionsResults = this.getChoices.results;
      this.optionsSort = this.getChoices.sort;
      this.setOptions()
    },

    methods: {

      setOptions() {
        let options = [this.optionsCommon, this.optionsResults, this.optionsSort];
        options.forEach(lis => {
          lis.unshift({'id': null, 'value': '--------'})
        })
      },

      handleClick(row) {
        console.log(row);
      },

      // 跳转查看url
      getUrl(url) {
        return this.$http.HOST + url
      },

      // 修改每页显示条数
      handleSizeChange(val) {
        this.pageSize = val;  // 修改每页显示条数
        this.currentPage = 1;  // 修改页码
      },
      // 修改页码
      handleCurrentChange(val) {
        this.currentPage = val;  // 修改页码
      },

      // 发送啊假克丝请求 得到表格的原始数据
      objList() {
        this.$myTool.objList(this)
      },

      // 删除函数，调用了删除接口
      delObjs(objId) {
        this.$myTool.delObjs(objId, this)
      },

      // 步骤提升函数
      stepsRealizeAscension(objId) {
        this.$http.getApiFunc(this.$http.getApiStatus)(this.$http.StepsRealizeAscensionURL, {id: objId})
          .then(res => {
            if (res.data.status) {
              _this.$alert(res.data.msg, '提示', {
                confirmButtonText: '确定'
              })
            }
            this.objList()
          })
          .catch(err => {
            console.log(err);
          })
      },

      //  按项目名称搜索的函数
      inputFunc() {
        this.$myTool.inputFunc(this)
      },

      // 搜索时使用的方法
      getInputData() {
        return {
          project_id: this.optionsProjectId[0] ? this.optionsProjectId[0] : null,
          module_id: this.optionsModuleId[0] ? this.optionsModuleId[0] : null,
          steps_id: this.optionsStepsId[0] ? this.optionsStepsId[0] : null,
          module_page_id: this.optionsModulePageId[0] ? this.optionsModulePageId[0] : null,
          selenium_func_id: this.optionsSeleniumFuncId[0] ? this.optionsSeleniumFuncId[0] : null,
          results: this.results,
          status: this.status,
          obj_key: this.obj_key,

          inputObjName: this.inputObjName
        }
      },

      // 项目信息搜索
      getProjectInfo(query) {
        this.$myTool.getObjList(this, this.$http.projectURL, this.project_info, query)
      },
      // 模块
      getModuleInfo(query) {
        let queryData = {
          inputObjName: query,
          module_project: this.optionsProjectId[0] ? this.optionsProjectId[0] : null,
        };
        this.$myTool.getObjList(this, this.$http.moduleURL, this.module_info, queryData)
      },

      // 获取页面信息
      getModulePageInfo(query) {
        let QuerySet = {
          module_page_module_id: this.optionsModuleId[0] ? this.optionsModuleId[0] : null,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.ModulePageURL, this.module_page_info, QuerySet)
      },

      // 获取步骤信息
      getStepsInfo(query) {
        let QuerySet = {
          module_page_module_id: this.optionsModuleId[0] ? this.optionsModuleId[0] : null,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.StepsURL, this.steps_info, QuerySet)
      },

      // 获取函数信息
      getSeleniumFuncInfo(query) {
        let QuerySet = {
          module_page_id: this.optionsModulePageId[0] ? this.optionsModulePageId[0] : null,
          inputObjName: query
        };
        this.$myTool.getObjList(this, this.$http.SeleniumFuncURL, this.selenium_func_info, QuerySet)
      },

      // 不能删
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },

    },

    watch: {

      'pageSize': function (value) {
        this.objList();
      },
      'currentPage': function (value) {
        this.objList();
      },

      optionsProjectId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },

      optionsModuleId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },

      optionsModulePageId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },

      optionsStepsId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },

      optionsSeleniumFuncId: function (value) {
        value = this.$myTool.delLenghtList(1, value, this);
      },

    },

    computed: {
      getChoices() {
        return this.$store.state.choicesInfo
      }
    },
  }
</script>

<style scoped>
  .paging {
    padding-top: 20px;
  }

  .input-data {
    float: left;
    width: 200px;
    padding: 5px 5px;
  }

  .mytable {
    /*width: 120px;*/
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

</style>
